package com.hddf.project.action;

import java.io.File;
import java.util.List;

import com.hddf.project.base.BaseAction;
import com.hddf.project.pojos.Project;
import com.hddf.project.pojos.ProjectApproval;
import com.hddf.project.pojos.ProjectCheck;
import com.hddf.project.pojos.ProjectDoc;
import com.hddf.project.pojos.ProjectImg;
import com.hddf.project.pojos.ProjectOther;
import com.hddf.project.pojos.ProjectTable;
import com.hddf.project.pojos.ProjectVideo;
import com.hddf.project.service.ProjectService;
import com.hddf.project.service.UploadService;
import com.hddf.project.upload_cache.CacheService;
import com.hddf.project.util.O;
import com.hddf.project.util.Stct;
import com.mongodb.DBObject;

public class UploadAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private File file;
	private Integer type, p_id;
	private String name, uuid, chunks, chunk;

	CacheService cas = CacheService.getInstance();

	/**
	 * 上传证书
	 */
	@SuppressWarnings("unchecked")
	public void addProject() {
		try {
			if (O.ckSt(name) && type != 0 && file != null && O.ckSt(uuid)) {
				DBObject obj = cas.getObjById(uuid);
				String path = (String) obj.get("path");
				if (!O.ckSt(path)) {// 等于空
					path = Stct.UPLOAD_PATH + O.uuid() + "/";
					cas.updatePath(uuid, path);
				}
				StringBuffer sb = new StringBuffer();
				sb.append(path);// 路径
				switch (type) {
				case Stct.UPLOAD_TYPE_DOC:
					sb.append(Stct.UPLOAD_NAME_DOC);
					break;
				case Stct.UPLOAD_TYPE_IMG:
					sb.append(Stct.UPLOAD_NAME_IMG);
					break;
				case Stct.UPLOAD_TYPE_TABLE:
					sb.append(Stct.UPLOAD_NAME_TABLE);
					break;
				case Stct.UPLOAD_TYPE_APPROVAL:
					sb.append(Stct.UPLOAD_NAME_APPROVAL);
					break;
				case Stct.UPLOAD_TYPE_CHECK:
					sb.append(Stct.UPLOAD_NAME_CHECK);
					break;
				case Stct.UPLOAD_TYPE_VIDEO:
					sb.append(Stct.UPLOAD_NAME_VIDEO);
					break;
				case Stct.UPLOAD_TYPE_OTHER:
					sb.append(Stct.UPLOAD_NAME_OTHER);
					break;
				}
				path = sb.toString();
				/**
				 * 判断与之前上传的是否重复
				 */
				String keyName = this.getKeyName();
				String result = cas.isFileExist(uuid, keyName, name);
				String fileName = "";
				if (O.ckSt(result)) {
					fileName = result.substring(result.lastIndexOf(":") + 1);
				} else {
					fileName = O.uuid() + name.substring(name.lastIndexOf("."));// 上传后的完整名字
				}

				UploadService us = UploadService.getInService();
				boolean tof = us.saveFiles(path, fileName, file);
				if (tof && !O.ckSt(result)) {// 保存成功
					List<String> strList = (List<String>) obj.get(keyName);
					strList.add(name + ":::" + fileName);
					cas.update(uuid, keyName, strList);
				}
			} else {
				state = "error";
				msg = "上传失败";
			}
		} catch (Exception e) {
			state = "error";
			msg = "操作失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 修改上传证书
	 */
	@SuppressWarnings("unchecked")
	public void updateProject() {
		try {
			if (O.ckSt(name) && type != 0 && p_id != 0 && file != null && O.ckSt(uuid)) {
				DBObject obj = cas.getObjById(uuid);
				ProjectService ps = ProjectService.getInService();
				Project p = (Project) ps.getObjectById(p_id, Project.class);
				if (p != null && 0 != p.getId()) {
					String path = "";
					if (O.ckSt(p.getPath())) {
						path = p.getPath();
						path = Stct.UPLOAD + path;
					} else {
						path = (String) obj.get("path");
						if (!O.ckSt(path)) {
							path = Stct.UPLOAD_PATH + O.uuid() + "/";
						}
					}
					String objPath = (String) obj.get(CacheService.PATH_PARAM);
					if (!O.ckSt(objPath)) {
						cas.updatePath(uuid, path);// 记录:调用过上传方法
					}
					StringBuffer sb = new StringBuffer();
					sb.append(path);// 路径
					switch (type) {
					case Stct.UPLOAD_TYPE_DOC:
						sb.append(Stct.UPLOAD_NAME_DOC);
						break;
					case Stct.UPLOAD_TYPE_IMG:
						sb.append(Stct.UPLOAD_NAME_IMG);
						break;
					case Stct.UPLOAD_TYPE_TABLE:
						sb.append(Stct.UPLOAD_NAME_TABLE);
						break;
					case Stct.UPLOAD_TYPE_APPROVAL:
						sb.append(Stct.UPLOAD_NAME_APPROVAL);
						break;
					case Stct.UPLOAD_TYPE_CHECK:
						sb.append(Stct.UPLOAD_NAME_CHECK);
						break;
					case Stct.UPLOAD_TYPE_VIDEO:
						sb.append(Stct.UPLOAD_NAME_VIDEO);
						break;
					case Stct.UPLOAD_TYPE_OTHER:
						sb.append(Stct.UPLOAD_NAME_OTHER);
						break;
					}
					path = sb.toString();

					/**
					 * 该图片在数据库中是否存在：如果存在则采用数据库中的名字，以便覆盖图片；如果不存在，则生成新名字，以便上传。
					 */
					boolean tof = true;
					String fileName = O.uuid() + name.substring(name.lastIndexOf("."));
					switch (type) {
					case Stct.UPLOAD_TYPE_DOC:
						ProjectDoc doc = ps.getDocByUploadName(p.getId(), name);
						if (null != doc && 0 != doc.getId()) {
							String docSrc = doc.getDocSrc();
							fileName = docSrc.substring(docSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					case Stct.UPLOAD_TYPE_IMG:
						ProjectImg img = ps.getImgByUploadName(p.getId(), name);
						if (null != img && 0 != img.getId()) {
							String imgSrc = img.getImgSrc();
							fileName = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					case Stct.UPLOAD_TYPE_TABLE:
						ProjectTable table = ps.getTableByUploadName(p.getId(), name);
						if (null != table && 0 != table.getId()) {
							String tableSrc = table.getTableSrc();
							fileName = tableSrc.substring(tableSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					case Stct.UPLOAD_TYPE_APPROVAL:
						ProjectApproval approval = ps.getApprovalByUploadName(p.getId(), name);
						if (null != approval && 0 != approval.getId()) {
							String approvalSrc = approval.getApprovalSrc();
							fileName = approvalSrc.substring(approvalSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					case Stct.UPLOAD_TYPE_CHECK:
						ProjectCheck check = ps.getCheckByUploadName(p.getId(), name);
						if (null != check && 0 != check.getId()) {
							String checkSrc = check.getCheckSrc();
							fileName = checkSrc.substring(checkSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					case Stct.UPLOAD_TYPE_VIDEO:
						ProjectVideo video = ps.getVideoByUploadName(p.getId(), name);
						if (null != video && 0 != video.getId()) {
							String videoSrc = video.getVideoSrc();
							fileName = videoSrc.substring(videoSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					case Stct.UPLOAD_TYPE_OTHER:
						ProjectOther po = ps.getOtherByUploadName(p.getId(), name);
						if (null != po && 0 != po.getId()) {
							String videoSrc = po.getOtherSrc();
							fileName = videoSrc.substring(videoSrc.lastIndexOf("/") + 1);
							tof = false;
						}
						break;
					}

					/**
					 * 判断与之前上传的是否重复
					 */
					String keyName = this.getKeyName();
					String result = cas.isFileExist(uuid, keyName, name);
					if (tof && O.ckSt(result)) {
						fileName = result.substring(result.lastIndexOf(":") + 1);
					}

					UploadService us = UploadService.getInService();
					boolean tof2 = us.saveFiles(path, fileName, file);
					if (tof2 && !O.ckSt(result) && tof) {// 保存成功
						List<String> strList = (List<String>) obj.get(keyName);
						strList.add(name + ":::" + fileName);
						cas.update(uuid, keyName, strList);
					}
				} else {
					state = "error";
					msg = "上传失败，项目不存在";
				}
			} else {
				state = "error";
				msg = "上传失败";
			}
		} catch (Exception e) {
			state = "error";
			msg = "操作失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取session的key的名字
	 * 
	 * @return
	 */
	public String getKeyName() {
		String keyName = "";
		switch (type) {
		case Stct.UPLOAD_TYPE_DOC:
			keyName = CacheService.DOCS_TYPE;
			break;
		case Stct.UPLOAD_TYPE_IMG:
			keyName = CacheService.IMGS_TYPE;
			break;
		case Stct.UPLOAD_TYPE_TABLE:
			keyName = CacheService.TABLES_TYPE;
			break;
		case Stct.UPLOAD_TYPE_APPROVAL:
			keyName = CacheService.APPROVAL_TYPE;
			break;
		case Stct.UPLOAD_TYPE_CHECK:
			keyName = CacheService.CHECKS_TYPE;
			break;
		case Stct.UPLOAD_TYPE_VIDEO:
			keyName = CacheService.VIDEO_TYPE;
			break;
		case Stct.UPLOAD_TYPE_OTHER:
			keyName = CacheService.OTHER_TYPE;
			break;
		}
		return keyName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public Integer getP_id() {
		return p_id;
	}

	public void setP_id(Integer p_id) {
		this.p_id = p_id;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public void setChunks(String chunks) {
		this.chunks = chunks;
	}

	public void setChunk(String chunk) {
		this.chunk = chunk;
	}

	public String getChunks() {
		return chunks;
	}

	public String getChunk() {
		return chunk;
	}

}